//================= OBSIDIAN CONFLICT(R) ==========================//
//
// Purpose: Draws the sniper rifle scope
//
//=============================================================================//

#include "cbase.h"
#include "hud.h"
#include "hudelement.h"
#include "iclientmode.h"
#include "hud_macros.h"

#include <vgui/IScheme.h>
#include <vgui/ISurface.h>
#include <vgui_controls/AnimationController.h>

#include "c_basehlplayer.h"

// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"

//-----------------------------------------------------------------------------
// Purpose: Draws the zoom screen
//-----------------------------------------------------------------------------
class CHudSniperZoom : public vgui::Panel, public CHudElement
{
	DECLARE_CLASS_SIMPLE( CHudSniperZoom, vgui::Panel );

public:
			CHudSniperZoom( const char *pElementName );

protected:
	virtual void ApplySchemeSettings(vgui::IScheme *scheme);
	virtual void Paint( void );
	
	CPanelAnimationVar( Color, m_RangeColor, "m_RangeColor", "255 255 255" );
	CPanelAnimationVarAliasType( float, digit_xpos, "digit_xpos", "50", "proportional_float" );
	CPanelAnimationVarAliasType( float, digit_ypos, "digit_ypos", "2", "proportional_float" );
	CPanelAnimationVar( vgui::HFont, m_hNumberGlowRangeFont, "NumberGlowRangeFont", "HudNumbersGlowRange" );

private:
	int		m_nSniperTextureID;
	int		m_nOICWTextureID;
	int		m_nXBowTextureID;
};

DECLARE_HUDELEMENT( CHudSniperZoom );

using namespace vgui;

//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
CHudSniperZoom::CHudSniperZoom( const char *pElementName ) : CHudElement(pElementName), BaseClass(NULL, "HudZoom")
{
	vgui::Panel *pParent = g_pClientMode->GetViewport();
	SetParent( pParent );

	// Scope textures
	m_nSniperTextureID = vgui::surface()->CreateNewTextureID();
	vgui::surface()->DrawSetTextureFile( m_nSniperTextureID, "sprites/sniper_scope" , true, false );
	m_nOICWTextureID = vgui::surface()->CreateNewTextureID();
	vgui::surface()->DrawSetTextureFile( m_nOICWTextureID, "sprites/oicw_scope" , true, false );
	m_nXBowTextureID = vgui::surface()->CreateNewTextureID();
	vgui::surface()->DrawSetTextureFile( m_nXBowTextureID, "sprites/xbow_scope" , true, false );

	SetHiddenBits( HIDEHUD_HEALTH | HIDEHUD_PLAYERDEAD );
}

//-----------------------------------------------------------------------------
// Purpose: sets scheme colors
//-----------------------------------------------------------------------------
void CHudSniperZoom::ApplySchemeSettings( vgui::IScheme *scheme )
{
	BaseClass::ApplySchemeSettings(scheme);

	SetPaintBackgroundEnabled(false);
	SetPaintBorderEnabled(false);

	SetFgColor( Color( 255, 255, 255, 255 ) );

	int screenWide, screenTall;
	GetHudSize(screenWide, screenTall);
	SetBounds(0, 0, screenWide, screenTall);
}

//-----------------------------------------------------------------------------
// Purpose: draws the zoom effect
//-----------------------------------------------------------------------------
void CHudSniperZoom::Paint( void )
{
	// see if we're zoomed any
	C_BaseHLPlayer *pPlayer = dynamic_cast<C_BaseHLPlayer *>( C_BasePlayer::GetLocalPlayer() );
	
	if ( pPlayer == NULL )
		return;

	int nScopeID = 0;

	if ( pPlayer->IsSniperZooming() )
		nScopeID = m_nSniperTextureID;
	else if ( pPlayer->IsOICWZooming() )
		nScopeID = m_nOICWTextureID;
	else if ( pPlayer->IsXBowZooming() )
		nScopeID = m_nXBowTextureID;

	// Not zooming?
	if ( !nScopeID )
		return;
	
	vgui::surface()->DrawSetColor( GetFgColor() );

	int wide, tall;
	GetSize( wide, tall );

	// Draws at 480x480 in a 640x480 resolution
	int scopeTopX = (wide/2) - scheme()->GetProportionalScaledValue( 240 );
	int scopeBtmX = (wide/2) + scheme()->GetProportionalScaledValue( 240 );

	int scopeTopY = (tall/2) - scheme()->GetProportionalScaledValue( 240 );
	int scopeBtmY = (tall/2) + scheme()->GetProportionalScaledValue( 240 );

	// Draw the Reticle
	vgui::surface()->DrawSetTexture( nScopeID );
	vgui::surface()->DrawTexturedRect( scopeTopX, scopeTopY, scopeBtmX, scopeBtmY );

	vgui::surface()->DrawSetColor( Color( 0, 0, 0, 255 ) );

	// Draw the fill around the edges
	int fillTopX = 0, fillBtmX = 0;
	int fillTopY = 0, fillBtmY = 0;

	for ( int i = 0; i < 4; i++ )
	{
		switch( i )
		{
			// Left side, top to bottom
			case 0:
			{
				fillTopX = 0;
				fillBtmX = scopeTopX;
				fillTopY = 0;
				fillBtmY = tall;
				break;
			}

			// Top middle
			case 1:
			{
				fillTopX = scopeTopX;
				fillBtmX = scopeBtmX;
				fillTopY = 0;
				fillBtmY = scopeTopY;
				break;
			}

			// Right side, top to bottom
			case 2:
			{
				fillTopX = scopeBtmX;
				fillBtmX = wide;
				fillTopY = 0;
				fillBtmY = tall;
				break;
			}

			// Bottom middle
			case 3:
			{
				fillTopX = scopeTopX;
				fillBtmX = scopeBtmX;
				fillTopY = scopeBtmY;
				fillBtmY = tall;
				break;
			}
		}

		vgui::surface()->DrawFilledRect( fillTopX, fillTopY, fillBtmX, fillBtmY );

		//OICW, draw range-finder + etc cool shit
		if(nScopeID == m_nOICWTextureID)
		{
			// draw our name
			surface()->DrawSetTextFont(m_hNumberGlowRangeFont);
			surface()->DrawSetTextColor(m_RangeColor);
			surface()->DrawSetTextPos(digit_xpos, digit_ypos);

			//
			surface()->DrawPrintText(L"279 YARDS", wcslen(L"279 YARDS"));
		}
	}
}